import axios from "axios"
import { ElMessage } from 'element-plus'

const requests = axios.create({
    // baseURL: "/api/v1/app",
    baseURL: "/app",
    timeout: 5000,
    withCredentials: true
})
requests.interceptors.request.use(config => {
    const tokenData = localStorage.getItem("token");
    const token = tokenData ? JSON.parse(tokenData).token : null;
    if (token) {
        config.headers.token = token;
    }
    // config.headers.Cookie = JSON.parse(localStorage.getItem("token&cookie") as string).cookie
    return config
}
)
requests.interceptors.response.use((res) => {
    if (res.data.code !== 0 && res.data.code !== 401) {
        ElMessage.error(res.data.msg || "请求失败！");
    }
    return res.data;
}, (err) => {
    ElMessage.error(err.message || "服务器异常！");
    return Promise.reject(err)
})
export default requests